package com.shanghaionstar.user;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.user.bean.ReplenishEntity;
import com.shanghaionstar.user.iview.ReplenishViewImpl;
import com.shanghaionstar.user.presenter.ReplenishPresenter;
import com.shanghaionstar.utils.ApplicationUtil;

/**
 * Info3.0补充用户信息页面
 * <p>
 * Created by Yonglang Hong on 2016/11/15.
 * 切换车辆用户信息界面
 */
@ContentView(R.layout.activity_replenishinfo3)
public class ReplenishInfo3Activity extends SwipeActivity {

    public static final int REPLENISH_SUCCESS = 1001;

    @FindViewById
    EditText edt_surname, edt_name;
    @FindViewById
    TextView tv_agreement;
    @FindViewById
    TextView tv_errInfo;
    @FindViewById
    RippleCustomBtn btn_sure;

    ReplenishPresenter replenishPresenter;

    ProgressDialog progressDialog;

    private void showLoading() {
        if (progressDialog == null){
            progressDialog = new ProgressDialog(this);
        }
        progressDialog.show();
    }

    private void hideLoading(){
        if (progressDialog != null)
            progressDialog.cancel();
    }

    public ReplenishPresenter getReplenishPresenter() {
        if (replenishPresenter == null) {
            replenishPresenter = new ReplenishPresenter(getScheduler());
            replenishPresenter.setView(new ReplenishViewImpl() {
                @Override
                public void onSuccess() {
                    hideLoading();
                    ApplicationUtil.getInstance().setFirstName(edt_surname.getText().toString());
                    ApplicationUtil.getInstance().setLastName(edt_name.getText().toString());
                    ToastUtil.show(ReplenishInfo3Activity.this, "补充成功");
                    finish();
                }
                @Override
                public void onFailed(Throwable throwable) {
                    hideLoading();
                    ToastUtil.show(ReplenishInfo3Activity.this, "补充失败");
                }
            });
        }
        return replenishPresenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        tv_agreement.setText(Html.fromHtml("<font color='#9c9da0' size='24px'>"
                + getResources().getString(R.string.read_check) + "</font><font color='#000000' size='24px'>"
                + getResources().getString(R.string.xieyi2) + "</font><font color='#9c9da0' size='24px'>"
                + getResources().getString(R.string.xieyi3) + "</font>"));
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replenishUserInfo();
            }
        });
        tv_agreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoTCPSAgreement();
            }
        });
        edt_surname.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    tv_errInfo.setText(R.string.nonull_first_name);
                    tv_errInfo.setVisibility(View.VISIBLE);
                }
                {
                    tv_errInfo.setVisibility(View.GONE);
                }
            }
        });
        edt_name.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    tv_errInfo.setText(R.string.nonull_sec_name);
                    tv_errInfo.setVisibility(View.VISIBLE);
                }
                {
                    tv_errInfo.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 跳转用户协议
     */
    private void gotoTCPSAgreement() {
//        Intent intent = new Intent(ReplenishInfo3Activity.this,CommonWebViewActivity.class);
//        Bundle bundle = new Bundle();
//        bundle.putString("topTitle","TCPS服务协议");
//        bundle.putString("linkUrl","WWW.baidu.com");
//        intent.putExtras(bundle);
//        startActivity(intent);
        WebDataEntity entity = new WebDataEntity();
        entity.setTopTitle("TCPS服务协议");
        entity.setLinkUrl("WWW.baidu.com");
        entity.setWhat(IWebType.TYPE_NORMAL);
        CommonWebViewActivity.launchCommonWeb(ReplenishInfo3Activity.this, entity);
    }

    /**
     * 完善用户信息
     */
    private void replenishUserInfo() {
        if (TextUtils.isEmpty(edt_name.getText())) {
            ToastUtil.show(ReplenishInfo3Activity.this, R.string.nonull_sec_name);
        } else if (TextUtils.isEmpty(edt_surname.getText())) {
            ToastUtil.show(ReplenishInfo3Activity.this, R.string.nonull_first_name);
        } else {
            tv_errInfo.setVisibility(View.GONE);
            ReplenishEntity entity = new ReplenishEntity();
            entity.setUsername(ApplicationUtil.getInstance().getAccountId());
            entity.setFirstName(edt_surname.getText().toString());
            entity.setLastName(edt_name.getText().toString());
            replenish(entity);
        }
    }

    private void replenish(ReplenishEntity entity){
        if (getIntent() != null && ChangeVehicleFragment.class.getSimpleName().equals(getIntent().getStringExtra("from"))){
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("replenishInfo",entity);
            intent.putExtras(bundle);
            setResult(REPLENISH_SUCCESS,intent);
            finish();
        }else {
            showLoading();
            getReplenishPresenter().replenish(entity);
        }
    }

    private class TextWatcherAdapter implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public BasePresenter getPresenter() {
        return replenishPresenter;
    }
}
